package Controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;

import View.CommentsReportView;
import Communication.Client;
import Communication.MessageHandle;
import DB.ReportQuery;
import Entity.CommentReport;
/** This class controls and manages the process that produce comment reports to the marketing manager */
public class CommentsReportController implements Serializable {
	private static final long serialVersionUID = 42L;
	/**  Required attributes */
	ReportQuery reportModel;
	Client client;
	CommentsReportView commentView;
	static CommentReport commentReport=new  CommentReport();


	/**
	 * Constructor calling to listener class that attached to the process
	 * @param client
	 * @param reportsController
	 */
	public CommentsReportController(Client client, ReportsController reportsController) {
		this.client = client;
		commentView =  new CommentsReportView();
		commentView.showWindow();			
		commentView.addCommentReportListeners(new commentReportsInfoListener(reportsController));
	}


	/**  Inner Listener class */ 
	class commentReportsInfoListener implements ActionListener
	{
		ReportsController reportsController;

		/**
		 * creats commentReportsInfo Listener
		 * @param reportsController
		 */
		public commentReportsInfoListener(ReportsController reportsController){
			this.reportsController = reportsController;
			reportModel = new ReportQuery();
			commentView.showWindow();
		}
		/**
		 * handle actions
		 */
		public void actionPerformed(ActionEvent event) {


			if (event.getSource() == commentView.getProduceBtn())
			{
				if( reportModel.isEmpty(CommentsReportView.getSaleRepId()))
				{
					SystemController.displayMessage("SaleID Field Is Missing!");

				}
				else{
					String SaleID=CommentsReportView.getSaleRepId();


					try {
						MessageHandle msg = new MessageHandle(SaleID, MessageHandle.COMMENT_REPORT, false);
						reportsController.hideWindow();

						client.sendToServer(msg);

					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace( );

					}

				}
			}
			else if(event.getSource() == commentView.getBackBtn())
			{  
				reportsController.showWindow();
				commentView.hideWindow();
			}

		}
	}



	/**
	 * handles the message received from server - if the DB was updated or not and why.
	 * @param msg
	 */
	public static void updataView(String msg){
		if (msg.compareTo("Success")==0)
			SystemController.displayMessage("Report successfully Created");

		else SystemController.displayErrorMessage("SaleID not Exist");

	}
}
